package cn.yuanqiao.web.controller.archive.qzj.controller;

import cn.yuanqiao.archive.archiveBorrow.service.IYqDaBorrowService;
import cn.yuanqiao.archive.qzj.domain.YqdaQzjInfo;
import cn.yuanqiao.archive.qzj.service.YqdaQzjInfoService;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.core.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.core.domain.R;
import cn.yuanqiao.common.core.page.TableDataInfo;
import cn.yuanqiao.common.enums.BusinessType;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/archive/qzjInfo")
public class YqdaQzjInfoController extends BaseController {
    @Autowired
    private YqdaQzjInfoService yqdaQzjInfoService;
    @Autowired
    private IYqDaBorrowService yqDaBorrowService;


    @Log(title = "查询全宗卷列表", businessType = BusinessType.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(YqdaQzjInfo yqdaQzjInfo){
        if(yqdaQzjInfo == null || yqdaQzjInfo.getTreeId()==null){
            throw new ServiceException("参数错误");
        }
        startPage();
        List<YqdaQzjInfo> list = yqdaQzjInfoService.selectList(yqdaQzjInfo);
        return getDataTable(list);
    }

    @Log(title = "查询全宗卷下载", businessType = BusinessType.DOWNLOAD)
    @PostMapping("/export/{treeId}")
    public void export(HttpServletResponse response, @PathVariable("treeId") Long treeId){
        YqdaQzjInfo yqdaQzjInfo = new YqdaQzjInfo();
        yqdaQzjInfo.setTreeId(treeId);
        List<YqdaQzjInfo> list = yqdaQzjInfoService.selectList(yqdaQzjInfo);
        ExcelUtil<YqdaQzjInfo> util = new ExcelUtil<YqdaQzjInfo>(YqdaQzjInfo.class);
        util.exportExcel(response, list, "全宗卷");
    }

    @Log(title = "获取全宗卷详细信息", businessType = BusinessType.SELECT)
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID)
    {
        return AjaxResult.success(yqdaQzjInfoService.getById(ID));
    }

    @Log(title = "添加全宗卷", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody YqdaQzjInfo yqdaQzjInfo){
        return AjaxResult.success(yqdaQzjInfoService.insertYqdaQzjInfo(yqdaQzjInfo));
    }

    @Log(title = "修改全宗卷", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YqdaQzjInfo yqdaQzjInfo){
        return AjaxResult.success(yqdaQzjInfoService.updateYqdaQzjInfo(yqdaQzjInfo));
    }

    @Log(title = "删除全宗卷", businessType = BusinessType.DELETE)
    @DeleteMapping("/del")
    public AjaxResult remove(@RequestBody JSONObject ID) {
        String[] idsStr = ID.getString("ids").split(",");
        Long[] ids = new Long[idsStr.length];
        for (int i = 0; i < idsStr.length; i++) {
            ids[i] = Long.valueOf(idsStr[i]);
        }
        return AjaxResult.success(yqdaQzjInfoService.del(ids));
    }
    /**
     * 修改archiveBorrow
     */
    @PostMapping("/updateAllStatus/{userId}")
    public R<Integer> updateAllStatus(@PathVariable("userId") Long userId) {
        return R.ok(yqDaBorrowService.updateAllStatus(userId));
    }

}
